package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import pac.Label;
import pac.Sql;

import tools.KeyCheck;

public class EditLabel extends HttpServlet {
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setHeader("Content-Type", "text/html; charset=UTF-8");
		PrintWriter out = resp.getWriter();
		JSONObject jsonObject = new JSONObject();
		try {
			Label label = Sql
					.getLabel(Integer.parseInt(req.getParameter("id")));
			if (label.sponsor_id != Integer.parseInt(KeyCheck.getId(req))) {
				throw new Exception();
			}
			label.kind = Integer.parseInt(req.getParameter("kind"));
			label.joining_limit = Integer.parseInt(req
					.getParameter("joining_limit"));

			label.place = req.getParameter("place");
			label.start_time = Long.parseLong(req.getParameter("start_time"));

			if (Sql.editeLabel(label)) {
				jsonObject.put("errcode", 0);
				out.println(jsonObject);
				out.flush();
				out.close();
			}

		} catch (Exception e) {
			jsonObject.put("errcode", 1);
			out.println(jsonObject);
			out.flush();
			out.close();
		}

	}
}
